package com.example.movie.ui.cinema.adapter

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.setPadding
import androidx.recyclerview.widget.RecyclerView
import com.example.movie.R
import com.example.movie.data.cinema.model.bean.CinemaInfo
import com.example.movie.ui.cinema.MoviePlanActivity
import com.example.movie.utils.SizeUtil
import kotlinx.android.synthetic.main.cinema_item.view.*

/**
 * 电影院列表信息适配器
 */
class CinemaItemAdapter(
    private val items: List<CinemaInfo>,
    private val requireContext: Context,
) : RecyclerView.Adapter<CinemaItemAdapter.CinemaItemViewHolder>() {

    class CinemaItemViewHolder(view: View) :
        RecyclerView.ViewHolder(view)

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): CinemaItemViewHolder {
        //创建viewHolder的时候设置这个布局的parent为参数中的parent，否则会出现没有铺满屏幕的问题
        return CinemaItemViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.cinema_item, parent, false))
    }


    override fun onBindViewHolder(holder: CinemaItemViewHolder, position: Int) {
        with(items[position]){
            with(holder.itemView){
                this.cinema_name.text = cinemaName
                this.lowest_price.text = lowestPrice.toString()
                this.cinema_location.text = location
                lowest_price.text = lowestPrice.toString()

                val layoutParams = LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                )
                //设置影院特性view结束边距
                layoutParams.marginEnd = SizeUtil.dip2px(context,5f)

                cinemaFeatures?.let { it ->
                    repeat(it.count()) {
                        with(cinemaFeatures[it]){
                            val feature = TextView(context)
                            feature.text = this
                            feature.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10f)
                            feature.gravity = Gravity.CENTER
                            feature.layoutParams = layoutParams
                            feature.setPadding(SizeUtil.dip2px(context,3f))
                            feature.setBackgroundResource(R.drawable.cinema_feature)

                            cinema__features.addView(feature)
                        }
                    } }

                cinema.setOnClickListener {
                    with(Bundle()){
                        this.putInt("agentId",cinemaId)
                        val intent = Intent(context, MoviePlanActivity::class.java)
                        intent.putExtras(this)
                        requireContext.startActivity(intent)
                    }
                }
            }
        }

    }

    override fun getItemCount() = items.size
}